package oj;

import java.util.HashMap;
import java.util.Map;

class Node2 {
    int val;
    Node2 next;
    Node2 random;

    public Node2(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
public class LC138 {
    public Node2 copyRandomList(Node2 head) {
        if(head==null) return null;
        Map<Node2, Node2> nodeMap=new HashMap<>();
        for(Node2 x = head; x!=null; x=x.next){
            Node2 newNode=new Node2(x.val);
            nodeMap.put(x,newNode);
        }

        for(Node2 x = head; x!=null; x=x.next){

            nodeMap.get(x).next=nodeMap.get(x.next);
            nodeMap.get(x).random=nodeMap.get(x.random);
        }
        return nodeMap.get(head);
    }
}
